import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class FMain {

    public FMain() {
        jFrame= new JFrame("Login");
        jFrame.setContentPane(rootPanel);
        jFrame.setSize(500,300);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //jFrame.pack();
        //设置jframe屏幕居中
        jFrame.setLocationRelativeTo(null);




        jFrame.setVisible(true);

        loadNameAndPwd();
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断cbRemember是否选中，选中则实现
                if(cbRemember.isSelected()){
                    //将账号和密码的数据保存到本地的“remember.bin”文件中
                    //打开当前目录的“remember.bin”文件
                    //创建文件对象
                    File file = new File("remember.bin");
                    try {
                        //创建文件输出流对象
                        FileOutputStream fos = new FileOutputStream(file);
                        //将账号和密码的数据写入到“remember.bin”文件中
                        fos.write((tfName.getText() + ":" + new String(pfPwd.getPassword())).getBytes());
                        //关闭文件输出流对象
                        fos.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    System.out.println("记住密码");
                }else{
                    System.out.println("不记住密码");
                    File file = new File("remember.bin");
                    //删除“remember.bin”文件
                    if (file.exists()) {
                        file.delete();
                    }
                }
                //
                Connection connection = JdbcHelper.getConnection();
                //使用"SELECT * FROM tb_users WHERE loginname = '用户名' AND password = '加密后的密码';"完成登录功能
                try {
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery("SELECT * FROM tb_users WHERE loginname = '"+tfName.getText()+"' AND password = '"+new String(pfPwd.getPassword())+"';");
                    if (resultSet.next()) {
                        JOptionPane.showMessageDialog(rootPanel,"登录成功");
                        //关闭登录窗口
                        new FLogin();
                        jFrame.setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(rootPanel,"用户名或者密码错误");
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }finally {
                    JdbcHelper.closeConnection(connection, null, null);
                }

            }
        });


    }
    private JFrame jFrame;


    private JPanel rootPanel;
    private JTextField tfName;
    private JCheckBox cbRemember;
    private JPasswordField pfPwd;
    private JButton btnLogin;
    private JLabel lblForget;

    public JPanel getRootPanel() {
        return rootPanel;
    }

    private void loadNameAndPwd() {
        //判断是否存在remimber.bin文件
        //创建文件
        File file = new File("remember.bin");
        BufferedReader reader = null;
        if(file.exists()){
            cbRemember.setSelected(true);
            try {
                //读取一行file数据
                reader = new BufferedReader(new FileReader(file));
                String line = reader.readLine();
                if (line != null) {
                    String[] parts = line.split(":");
                    tfName.setText(parts[0]);
                    pfPwd.setText(parts[1]);
                }
                if(reader != null) {
                    reader.close();
                }



            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {

            }
        }
        

    }


}
